package com.hornsun.data.repository;

import com.hornsun.data.dbo.Sysinfo;
import com.hornsun.data.dbo.SysinfoExample;
import com.hornsun.data.repository.mapper.SysinfoMapper;
import com.hornsun.util.RSAUtils;
import com.hornsun.util.SerializeUtils;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by 编译中... on 2018/7/11.
 */
@Repository
public class SysinfoRepository {

    @Autowired
    private SysinfoMapper sysinfoMapper;

    //获取系统信息
    public Sysinfo getSysinfo(){
        List<Sysinfo> sysinfos = sysinfoMapper.selectByExample(new SysinfoExample());
        if (sysinfos.size()==1){
            return sysinfos.get(0);
        }else {
            throw new RuntimeException("not found Sysinfo");
        }
    }

    //获取授权点数
    public Integer getAuthPoint() throws Exception {
        Sysinfo sysinfo = getSysinfo();
        return (int) SerializeUtils.deSerialize(RSAUtils.decryptByPublicKey(Base64.decodeBase64(sysinfo.getAuthorizationCount()), RSAUtils.getPublicKey()));
    }
}
